package com.bh.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.StringUtils;
import com.bh.extend.domain.JcDbProduct;
import com.bh.extend.service.IJcDbProductService;
import com.bh.order.domain.JcOrder;
import com.bh.order.domain.JcOrderProduct;
import com.bh.order.mapper.JcOrderMapper;
import com.bh.order.mapper.JcOrderProductMapper;
import com.bh.order.service.IJcOrderProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 订单货品明细Service业务层处理
 *
 * @author slx
 * @date 2023-04-22
 */

@Service
public class JcOrderProductServiceImpl extends ServiceImpl<JcOrderProductMapper, JcOrderProduct> implements IJcOrderProductService {
    @Autowired
    private JcOrderProductMapper jcOrderProductMapper;
    @Autowired
    private JcOrderMapper jcOrderMapper;
    @Autowired
    private IJcDbProductService jcDbProductService;

    /**
     * 查询订单货品明细
     *
     * @param id 订单货品明细主键
     * @return 订单货品明细
     */
    @Override
    public JcOrderProduct selectJcOrderProductById(Long id) {
        return jcOrderProductMapper.selectById(id);
    }

    /**
     * 查询订单货品明细列表
     *
     * @param jcOrderProduct 订单货品明细
     * @return 订单货品明细
     */
    @Override
    public List<JcOrderProduct> selectJcOrderProductList(JcOrderProduct jcOrderProduct) {
        return jcOrderProductMapper.selectJcOrderProductList(jcOrderProduct);
    }

    @Override
    public List<JcOrderProduct> selectJcOrderProductListByOrderId(Long orderId) {
        JcOrderProduct jcOrderProduct = new JcOrderProduct();
        jcOrderProduct.setJcOrderId(orderId);
        return jcOrderProductMapper.selectJcOrderProductList(jcOrderProduct);
    }

    /**
     * 新增订单货品明细
     *
     * @param jcOrderProduct 订单货品明细
     * @return 结果
     */
    @Override
    public int insertJcOrderProduct(JcOrderProduct jcOrderProduct, Long customerId) {
        jcOrderProduct.setCreateTime(DateUtils.getNowDate());
        jcOrderProduct.setCreateName(SecurityUtils.getUsername());
        int num = jcOrderProductMapper.insert(jcOrderProduct);
        // 修改数量
        JcOrder order = jcOrderMapper.selectById(jcOrderProduct.getJcOrderId());
        if (jcOrderProduct.getNumber() != null) {
            order.setNumber(order.getNumber() + jcOrderProduct.getNumber());
        }
        if (jcOrderProduct.getWeight() != null) {
            order.setWeight(order.getWeight().add(jcOrderProduct.getWeight()));
        }
        if (jcOrderProduct.getValue() != null) {
            order.setValue(order.getValue().add(jcOrderProduct.getValue()));
        }
        if (jcOrderProduct.getVolume() != null) {
            order.setVolume(order.getVolume().add(jcOrderProduct.getVolume()));
        }
        jcOrderMapper.updateById(order);
        if (num > 0) {
            insertProduct(jcOrderProduct, customerId);
        }
        return num;
    }


    void insertProduct(JcOrderProduct jcOrderProduct, Long customerId){
        if (StringUtils.isNotEmpty(jcOrderProduct.getName()) && StringUtils.isNotEmpty(jcOrderProduct.getUnit())) {
            // 保存产品
            JcDbProduct jcDbProduct = new JcDbProduct();
            jcDbProduct.setName(jcOrderProduct.getName());
            jcDbProduct.setUnit(jcOrderProduct.getUnit());
            jcDbProduct.setCustomerId(customerId);
            List<JcDbProduct> products = jcDbProductService.selectJcDbProductList(jcDbProduct);
            if (products.size() == 0) {
                jcDbProduct.setCreateTime(new Date());
                jcDbProduct.setCreateBy(jcOrderProduct.getCreateName());
                jcDbProductService.insertJcDbProduct(jcDbProduct);
            }
        }
    }

    /**
     * 修改订单货品明细
     *
     * @param jcOrderProduct 订单货品明细
     * @return 结果
     */
    @Override
    public int updateJcOrderProduct(JcOrderProduct jcOrderProduct) {
        jcOrderProduct.setModifyTime(DateUtils.getNowDate());
        jcOrderProduct.setModifyName(SecurityUtils.getUsername());
        JcOrderProduct old = selectJcOrderProductById(jcOrderProduct.getId());
        // 修改数量
        JcOrder order = jcOrderMapper.selectById(jcOrderProduct.getJcOrderId());
        order.setNumber(order.getNumber() - old.getNumber() + jcOrderProduct.getNumber());
        order.setWeight(order.getWeight().subtract(old.getWeight()).add(jcOrderProduct.getWeight()));
        order.setValue(order.getValue().subtract(old.getValue()).add(jcOrderProduct.getValue()));
        order.setVolume(order.getVolume().subtract(old.getVolume()).add(jcOrderProduct.getVolume()));
        jcOrderMapper.updateById(order);

        return jcOrderProductMapper.updateById(jcOrderProduct);
    }

    /**
     * 批量删除订单货品明细
     *
     * @param ids 需要删除的订单货品明细主键
     * @return 结果
     */
    @Override
    public int deleteJcOrderProductByIds(Long[] ids) {
        // 修改数量
        List<JcOrderProduct> olds = jcOrderProductMapper.selectBatchIds(Arrays.asList(ids));
        for (JcOrderProduct jcOrderProduct : olds) {
            JcOrder order = jcOrderMapper.selectById(jcOrderProduct.getJcOrderId());
            order.setNumber(order.getNumber() - jcOrderProduct.getNumber());
            order.setWeight(order.getWeight().subtract(jcOrderProduct.getWeight()));
            order.setValue(order.getValue().subtract(jcOrderProduct.getValue()));
            order.setVolume(order.getVolume().subtract(jcOrderProduct.getVolume()));
            jcOrderMapper.updateById(order);
        }

        return jcOrderProductMapper.deleteBatchIds(Arrays.asList(ids));
    }
}
